/*
 * @# AlbumsController.java 2011-12-23 下午01:21:31
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.album.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ianswer.common.util.Feedback;
import com.ianswer.common.util.web.ServletUtils;
import com.ianswer.px.album.entity.Album;
import com.ianswer.px.album.service.AlbumService;
import com.ianswer.px.album.service.GalleryService;
import com.ianswer.px.common.SessionContextHolder;
import com.ianswer.px.common.annotation.SessionAware;

/**
 * 相册维护, 主要是相册本身的.
 * 
 * @author alvin hwang
 */
@SessionAware
@Controller
public class AlbumsController {

	@Resource(name = "albumService")
	private AlbumService albumService;
	
	@Resource(name = "galleryService")
	private GalleryService galleryService;
	
	/**
	 * 圈内相册
	 */
	@RequestMapping(value = "/albums", method = RequestMethod.GET)
	public String index() {
		return "album/index";
	}
	
	/**
	 * 圈内相册
	 */
	@RequestMapping(value = "/albums/list", method = RequestMethod.GET)
	public @ResponseBody List<Album> list() {
		Long userId = SessionContextHolder.getUserId();
		return albumService.queryShowAlbums(userId);
	}

	/**
	 * 博文配图
	 */
	@RequestMapping(value = "/albums/posts", method = RequestMethod.GET)
	public String posts() {
		return "album/posts";
	}

	/**
	 * 某个分类下的秀场相册
	 */
	@RequestMapping(value = "/albums/gallery/{galleryId}/shows", method = RequestMethod.GET)
	public String galleryShows(@PathVariable("galleryId") Long galleryId, Model model) {
		Long userId = SessionContextHolder.getUserId();
		model.addAttribute("albums", albumService.queryShowAlbums(userId, galleryId));
		model.addAttribute("galleries", galleryService.queryShowGalleries(userId));
		return "album/shows";
	}
	
	/**
	 * 秀场
	 */
	@RequestMapping(value = "/albums/shows", method = RequestMethod.GET)
	public String shows(Model model) {
		Long userId = SessionContextHolder.getUserId();
		model.addAttribute("albums", albumService.queryLastestShowAlbums(userId));
		model.addAttribute("galleries", galleryService.queryShowGalleries(userId));
		return "album/shows";
	}
	
	@RequestMapping(value = "/albums/{albumId}/edit", method = RequestMethod.GET)
	public @ResponseBody Album editAlbum(@PathVariable("albumId") Long albumId) {
		Album album = albumService.get(albumId);
		return album;
	}
	
	// 更新保存
	@RequestMapping(value = "/albums/{albumId}/edit", method = RequestMethod.POST)
	public @ResponseBody Feedback updateAlbum(Album album, @PathVariable("albumId") Long albumId) {
		Long userId = SessionContextHolder.getUserId();
		album.setUserId(userId);
		albumService.update(album);
		return Feedback.success("t");
	}
	
	// 新增秀场专辑保存
	@RequestMapping(value = "/albums/shows/create", method = RequestMethod.POST)
	public @ResponseBody Feedback saveShowsAlbum(Album album, HttpServletRequest request) {
		Long userId = SessionContextHolder.getUserId();
		album.showType().setUserId(userId);
		album.setIpAddr(ServletUtils.getIpAddrLong(request));
		Long albumId = albumService.insert(album);
		return Feedback.success("t").setObj(albumId);
	}
	
	// 新增名品展示专辑保存
	@RequestMapping(value = "/albums/dress/create", method = RequestMethod.POST)
	public @ResponseBody Feedback saveDressAlbum(Album album, HttpServletRequest request) {
		Long userId = SessionContextHolder.getUserId();
		album.dressType().setUserId(userId);
		album.setIpAddr(ServletUtils.getIpAddrLong(request));
		Long albumId = albumService.insert(album);
		return Feedback.success("t").setObj(albumId);
	}

	@RequestMapping(value = "/albums/{albumId}/remove", method = RequestMethod.POST)
	public @ResponseBody Feedback removeAlbum(@PathVariable("albumId") Long albumId) {
		Long userId = SessionContextHolder.getUserId();
		albumService.delete(userId, albumId);
		return Feedback.success("t");
	}
	
	/**
	 * 名品展示
	 */
	@RequestMapping(value = "/albums/dress", method = RequestMethod.GET)
	public String photos() {
		return "album/dress";
	}
	
}
